/*
  Copyright 2012 Sergey Ostanin

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/

package ru.irenproject;

import javax.inject.Inject;

import ru.irenproject.it.ItInput;
import ru.irenproject.it.ItInputException;
import ru.irenproject.it.ItOutput;
import ru.irenproject.store.AbstractThing;
import ru.irenproject.store.Event;
import ru.irenproject.store.Stash;
import ru.irenproject.store.Thing;

import com.google.protobuf.Message;

public final class QuestionItemEditor extends AbstractThing implements Visual {
  public static final class GetModifiersOutput {
    public byte[] modifiers;
  }

  public static final class SetModifiersInput {
    public byte[] modifiers;
  }

  public static QuestionItemEditor create(QuestionItem item) {
    QuestionItemEditor res = item.store().create(QuestionItemEditor.class);
    res.prepare(item);
    return res;
  }

  @Stash private UiProtos.QuestionItemEditor.Builder $;
  private final QuestionBriefProvider fQuestionBriefProvider;
  private final ItOutput fItOutput;
  private final ItInput fItInput;

  @Inject QuestionItemEditor(QuestionBriefProvider questionBriefProvider,
      ItOutput itOutput, ItInput itInput) {
    fQuestionBriefProvider = questionBriefProvider;
    fItOutput = itOutput;
    fItInput = itInput;
  }

  private void prepare(QuestionItem item) {
    $.setItem(item.id());
    item.addListener(this);
    item.modifierList().addListener(this);

    Brief brief = fQuestionBriefProvider.create(item.question());
    $.setBrief(brief.id());
    brief.addListener(this);
  }

  public QuestionItem item() {
    return get($.getItem(), QuestionItem.class);
  }

  private Brief brief() {
    return get($.getBrief(), Brief.class);
  }

  @Override public Message render() {
    QuestionItem item = item();
    return UiProtos.QuestionItemEditorShape.newBuilder()
        .setText(brief().text())
        .setWeight(item.weight())
        .setQuestionKind(item.questionKind().toByteString())
        .setEnabled(item.enabled())
        .setHasModifiers(!item.modifierList().isEmpty())
        .build();
  }

  @Override protected void handleEvent(Event e, Thing source) {
    if ((e instanceof QuestionItem.ChangeEvent) || (e instanceof Brief.ChangeEvent)
        || (e instanceof ModifierList.ChangeEvent)) {
      markForRender();
    }
  }

  @Action(markAsModified = false)
  public GetModifiersOutput doGetModifiers() {
    GetModifiersOutput res = new GetModifiersOutput();
    res.modifiers = fItOutput.modifierListToBytes(item().modifierList());
    return res;
  }

  @Action(badInput = ItInputException.class)
  public void doSetModifiers(SetModifiersInput in) {
    fItInput.bytesToModifierList(Check.inputNotNull(in.modifiers), item().modifierList());
  }
}
